//
//  HomeStatistics3PLVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2021/6/23.
//  Copyright © 2021 吕晶. All rights reserved.
//

#import "HomeStatistics3PLVC.h"
#import "LJOneAddressPicker.h"
#import "StatisticsSelectAddressBtnView.h"
#import "Statistics3PLModel.h"
#import "Statistics3PLCell.h"
#import "HomeStatistics3PLDetailVC.h"

@interface HomeStatistics3PLVC () <UITableViewDelegate,UITableViewDataSource> {
    int page;
}

@property (nonatomic, strong) NSArray <LJOneAddressModel *>*addressArray;
@property (nonatomic, strong) StatisticsSelectAddressBtnView *shengView;
@property (nonatomic, strong) LJOneAddressModel *selectProvince;
@property (nonatomic, strong) LJOneAddressModel *selectCity;
@property (nonatomic, strong) StatisticsSelectAddressBtnView *shiView;
@property (nonatomic, strong) NSMutableArray <Statistics3PLModel *>*dataArray;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation HomeStatistics3PLVC

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(15, 15 + 36 + 15, ScreenW - 30, ScreenH - NaviHeight - (15 + 36 + 15))];
        _tableView.backgroundColor = HexColor(0xffffff);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.layer.masksToBounds = YES;
        _tableView.layer.cornerRadius = 5;
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page = 1;
            [strongSelf requestForList];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page++;
            [strongSelf requestForList];
        }];
    }
    return _tableView;
}

-(NSMutableArray<Statistics3PLModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(LJOneAddressModel *)selectProvince {
    if (!_selectProvince) {
        _selectProvince = [LJOneAddressModel new];
    }
    return _selectProvince;
}

-(LJOneAddressModel *)selectCity {
    if (!_selectCity) {
        _selectCity = [LJOneAddressModel new];
    }
    return _selectCity;
}

-(StatisticsSelectAddressBtnView *)shengView {
    if (!_shengView) {
        _shengView = [[StatisticsSelectAddressBtnView alloc] initWithFrame:CGRectMake(15, 15, (ScreenW - 40) * 0.5, 36)];
        _shengView.titleStr = @"省份";
        __weak_self;
        _shengView.clickBlock = ^{
            LJOneAddressPicker *picker = [LJOneAddressPicker new];
            picker.dataArray = weakself.addressArray;
            picker.finishBlock = ^(LJOneAddressModel * _Nullable address) {
                weakself.selectProvince = address;
                weakself.shengView.titleStr = address.name;
                weakself.shiView.titleStr = @"城市";
                weakself.selectCity = [LJOneAddressModel new];
                __strong_self;
                strongSelf->page = 1;
                [strongSelf requestForList];
            };
            [picker pickerViewShow];
        };
    }
    return _shengView;
}

-(StatisticsSelectAddressBtnView *)shiView {
    if (!_shiView) {
        _shiView = [[StatisticsSelectAddressBtnView alloc] initWithFrame:CGRectMake(self.shengView.nim_right + 10, 15, (ScreenW - 40) * 0.5, 36)];
        _shiView.titleStr = @"城市";
        __weak_self;
        _shiView.clickBlock = ^{
            if (!weakself.selectProvince.Id) {
                [MBProgressHUD leShowFailedHUD:weakself.view message:@"请先选择省份"];
                return;
            }
            LJOneAddressPicker *picker = [LJOneAddressPicker new];
            picker.dataArray = weakself.selectProvince.children;
            picker.finishBlock = ^(LJOneAddressModel * _Nullable address) {
                weakself.selectCity = address;
                weakself.shiView.titleStr = address.name;
                __strong_self;
                strongSelf->page = 1;
                [strongSelf requestForList];
            };
            [picker pickerViewShow];
        };
    }
    return _shiView;
}

-(NSArray<LJOneAddressModel *> *)addressArray {
    if (!_addressArray) {
        _addressArray = [NSArray array];
    }
    return _addressArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"已入驻3PL";
    [self requestForAddressData];
    [self.view addSubview:self.shengView];
    [self.view addSubview:self.shiView];
    [self.view addSubview:self.tableView];
    page = 1;
    [self requestForList];
}

- (void)requestForList {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@(page) forKey:@"page"];
    if (self.selectProvince.Id) {
        [dic setObject:self.selectProvince.Id forKey:@"province"];
    }
    if (self.selectCity.Id) {
        [dic setObject:self.selectCity.Id forKey:@"city"];
    }
    [HttpRequest PostWithApi:LogisticsSupplierHome3PLList_V_1_0 params:dic progress:^(NSProgress * _Nonnull progress) {

    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            if (self->page == 1) {
                [self.dataArray removeAllObjects];
                self.dataArray = [Statistics3PLModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
            }else {
                NSArray *array = [Statistics3PLModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                [self.dataArray addObjectsFromArray:array];
            }
            BOOL has_more = [responseObject[@"has_more"] boolValue];
            if (has_more) {
                self.tableView.mj_footer.hidden = NO;
            }else {
                self.tableView.mj_footer.hidden = YES;
            }
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)requestForAddressData {
    [MBProgressHUD leShowHUDWithMessage:@"加载中" activity:YES];
    self.shengView.userInteractionEnabled = NO;
    self.shiView.userInteractionEnabled = NO;
    [HttpRequest PostWithApi:LogisticsSupplierAddressList_V_1_0 params:@{} progress:^(NSProgress * _Nonnull progress) {

    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.addressArray = [LJOneAddressModel mj_objectArrayWithKeyValuesArray:responseObject];
        }
        [MBProgressHUD hidenHUD];
        self.shengView.userInteractionEnabled = YES;
        self.shiView.userInteractionEnabled = YES;
    } fail:^(NSError * _Nonnull error) {
        [MBProgressHUD hidenHUD];
        self.shengView.userInteractionEnabled = YES;
        self.shiView.userInteractionEnabled = YES;
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Statistics3PLCell *cell = [Statistics3PLCell Statistics3PLCellWithTableView:tableView];
    Statistics3PLModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    Statistics3PLCell *cell = [Statistics3PLCell Statistics3PLCellWithTableView:tableView];
    Statistics3PLModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell.height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    Statistics3PLModel *model = self.dataArray[indexPath.row];
    HomeStatistics3PLDetailVC *vc = [HomeStatistics3PLDetailVC new];
    vc.Id = model.Id;
    vc.name = model.name;
    [self.rt_navigationController pushViewController:vc animated:YES];
}

@end
